/*
 * app_file_event_link.c
 *
 *  Created on: 2023年8月24日
 *      Author: HXF
 */
#include "app_file_event_link.h"
#include "em_core.h"
#include "string.h"
#include "stdlib.h"
#include "app_log.h"
#include "link.h"

static link_t *file_link1_head = NULL;
#ifdef HUB_DEVICE
static link_t *file_link2_head = NULL;
static link_t *flie_history_head = NULL;
#endif
static link_t *file_event_head = NULL;

void file_link_init(void)
{
  CORE_DECLARE_IRQ_STATE;
  CORE_ENTER_CRITICAL();

  file_link1_head = link_creathead();
#ifdef HUB_DEVICE
  file_link2_head = link_creathead();
  flie_history_head = link_creathead();
#endif
  file_event_head = link_creathead();
  CORE_EXIT_CRITICAL();
}

int file_cache_link_push(FILE_LINK_NUM_E num, unsigned char *ptr_file, int len)
{
  int iret = 0xff;
  CORE_DECLARE_IRQ_STATE;
  CORE_ENTER_CRITICAL();
  if(num == FILE_LINK_1){
      iret = link_push_end(file_link1_head, ptr_file, len);
  }
#ifdef HUB_DEVICE
  else if(num == FILE_LINK_2){
      iret = link_push_end(file_link2_head, ptr_file, len);
  }else if(num == FILE_LINK_HISTORY){
      iret = link_push_end(flie_history_head, ptr_file, len);
  }
#endif
  CORE_EXIT_CRITICAL();
  return iret;
}

int file_cache_link_pop(FILE_LINK_NUM_E num, unsigned char *ptr_file, int *ptr_len)
{
  int iret = 0xff;
  CORE_DECLARE_IRQ_STATE;
  CORE_ENTER_CRITICAL();
  if(num == FILE_LINK_1){
      iret = link_pop(file_link1_head, ptr_file, ptr_len);
  }
#ifdef HUB_DEVICE
  else if(num == FILE_LINK_2){
      iret = link_pop(file_link2_head, ptr_file, ptr_len);
  }else if(num == FILE_LINK_HISTORY){
      iret = link_pop(flie_history_head, ptr_file, ptr_len);
  }
#endif
  CORE_EXIT_CRITICAL();
  return iret;
}

int file_cache_link_find_by_idx(FILE_LINK_NUM_E num,
                                unsigned char *ptr_file, int *ptr_len, int idx)
{
  int iret = 0xff;
  CORE_DECLARE_IRQ_STATE;
  CORE_ENTER_CRITICAL();
  if(num == FILE_LINK_1){
      iret = link_find_by_idx(file_link1_head, ptr_file, ptr_len, idx);
  }
#ifdef HUB_DEVICE
  else if(num == FILE_LINK_2){
      iret = link_find_by_idx(file_link2_head, ptr_file, ptr_len, idx);
  }else if(num == FILE_LINK_HISTORY){
      iret = link_find_by_idx(flie_history_head, ptr_file, ptr_len, idx);
  }
#endif
  CORE_EXIT_CRITICAL();
  return iret;
}

int file_cache_link_find_by_pageid(FILE_LINK_NUM_E num,
                                   unsigned char *ptr_file, int *ptr_len, unsigned char *pageid)
{
  int iret = 0xff;
  int link_len = 0;
  int idx = 0;
  cache_page_file_t *file = NULL;

  link_len = file_cache_link_len(num);

  while(idx < link_len){
    if(!file_cache_link_find_by_idx(num, ptr_file, ptr_len, idx)){
        file = (cache_page_file_t *)ptr_file;
        if((file->page_id[0] == pageid[0]) && (file->page_id[1] == pageid[1])){
            iret = 0;
            break;
        }
    }else{
        iret = 1;
        break;
    }
    idx++;
  }
  if(idx == link_len){
      iret = 1;
  }
  return iret;
}

int file_cache_link_len(FILE_LINK_NUM_E num)
{
  int iret = 0;
  CORE_DECLARE_IRQ_STATE;
  CORE_ENTER_CRITICAL();
  if(num == FILE_LINK_1){
      iret = link_len(file_link1_head);
  }
#ifdef HUB_DEVICE
  else if(num == FILE_LINK_2){
      iret = link_len(file_link2_head);
  }else if(num == FILE_LINK_HISTORY){
      iret = link_len(flie_history_head);
  }
#endif
  CORE_EXIT_CRITICAL();
  return iret;
}

int file_cache_link_delte(FILE_LINK_NUM_E num)
{
  int iret = 0xff;
  CORE_DECLARE_IRQ_STATE;
  CORE_ENTER_CRITICAL();
  if(num == FILE_LINK_1){
      iret = link_delte_all_node(file_link1_head);
  }
#ifdef HUB_DEVICE
  else if(num == FILE_LINK_2){
      iret = link_delte_all_node(file_link2_head);
  }else if(num == FILE_LINK_HISTORY){
      iret = link_delte_all_node(flie_history_head);
  }
#endif
  CORE_EXIT_CRITICAL();
  return iret;
}

bool file_cache_link_is_empty(FILE_LINK_NUM_E num)
{
  bool bret = false;
  CORE_DECLARE_IRQ_STATE;
  CORE_ENTER_CRITICAL();
  if(num == FILE_LINK_1){
      if(!file_link1_head){
          bret = true;
      }else{
          if(!file_link1_head->next){
              bret = true;
          }
      }
  }
#ifdef HUB_DEVICE
  else if(num == FILE_LINK_2){
      if(!file_link2_head){
          bret = true;
      }else{
          if(!file_link2_head->next){
              bret = true;
          }
      }
  }else if(num == FILE_LINK_HISTORY){
      if(!flie_history_head){
          bret = true;
      }else{
          if(!flie_history_head->next){
              bret = true;
          }
      }
  }
#endif
  else{
      bret = true;
  }
  CORE_EXIT_CRITICAL();
  return bret;
}

int file_evt_link_push(FILE_EVENT_E *evt)
{
  int iret = 0xff;
  CORE_DECLARE_IRQ_STATE;
  CORE_ENTER_CRITICAL();
  iret = link_push_end(file_event_head, (char *)evt, 1);
  CORE_EXIT_CRITICAL();
  return iret;
}

bool file_evt_link_is_empty(void)
{
  bool bret = false;
  CORE_DECLARE_IRQ_STATE;
  CORE_ENTER_CRITICAL();
  if(!file_event_head){
      bret = true;
  }else{
      if(!file_event_head->next){
          bret = true;
      }
  }
  CORE_EXIT_CRITICAL();
  return bret;
}

int file_evt_link_pop(FILE_EVENT_E *evt)
{
  int iret = 0xff;
  int len = 0;
  CORE_DECLARE_IRQ_STATE;
  CORE_ENTER_CRITICAL();
  iret = link_pop(file_event_head, (char *)evt, &len);
  CORE_EXIT_CRITICAL();
  return iret;
}
